package com.slofzx.nuoda.evaluation.version.builder;

import com.slofzx.nuoda.common.util.DataConvertUtil;
import com.slofzx.nuoda.evaluation.param.vo.CommonParamDataVO;
import com.slofzx.nuoda.evaluation.param.vo.CommonYearRelatedVO;
import com.slofzx.nuoda.evaluation.project.enums.*;
import com.slofzx.nuoda.evaluation.project.vo.*;
import com.slofzx.nuoda.evaluation.ref.builder.AbstractComputeInputDataBuilder;
import com.slofzx.nuoda.evaluation.ref.enums.ModelInputItem;

import java.util.*;

/**
 * 计算所需输入数据构建器
 * @author sunfuyong
 */
public class ComputeInputDataBuilder extends AbstractComputeInputDataBuilder {

    /**
     * 项目详情数据
     */
    private ProjectInfoVO projectDetailData;

    /**
     * 公共参数数据
     */
    private CommonParamDataVO commonParamData;

    /**
     * 任务公共参数
     */
    private ComputeInfoDataVO computeInfoDataVO;
    private BatchQualityEnum batchQuality;
    private ComputeInfoDataVO planInfo;


    public ComputeInputDataBuilder projectDetailData(ProjectInfoVO projectDetailData) {
        this.projectDetailData = projectDetailData;
        return this;
    }
    public ComputeInputDataBuilder planInfo(ComputeInfoDataVO planInfo) {
        this.planInfo = planInfo;
        return this;
    }
    public ComputeInputDataBuilder batchQuality(BatchQualityEnum batchQuality) {
        this.batchQuality = batchQuality;
        return this;
    }
    public ComputeInputDataBuilder commonParamData(CommonParamDataVO commonParamData) {
        this.commonParamData = commonParamData;
        return this;
    }

    public ComputeInputDataBuilder computeInfoDataVO(ComputeInfoDataVO computeInfoDataVO) {
        this.computeInfoDataVO = computeInfoDataVO;
        return this;
    }



    @Override
    protected Map<String, Object> getInputDataSourceMap() {
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("project-param",this.projectDetailData);
        if(projectDetailData!=null) {
            Map<Integer, ProjectYearDataVO> projectYearDataVOMap = new HashMap<>();
             Integer productionDay = projectDetailData.getProductionDay();
            int i = 0;
            for (ProjectYearDataVO item : projectDetailData.getYearDataList()) {
                // 去除合计列
                if (item.getYear()==0) {
                    continue;
                }
                Integer productionDayYear = 300;
                if (i == 0 && productionDay!=null) {
                    productionDayYear = productionDay;
                }
                item.setProductionDay(productionDayYear);
                i++;
                projectYearDataVOMap.put(DataConvertUtil.parse2Int(projectDetailData.getYear()) + item.getYear()-1 , item);
            }
            resultMap.put("project-param-year", projectYearDataVOMap);
            resultMap.put("project-cost-param", projectDetailData.getProjectCostData());
        }
        resultMap.put("evaluation-common-param",commonParamData);
        resultMap.put("batch-common-param",computeInfoDataVO);
        if(computeInfoDataVO!=null&&computeInfoDataVO.getYearDataList()!=null) {
            Map<Integer, BatchCommonDataVO> batchYearDataMap = new HashMap<>();
            for (BatchCommonDataVO item : computeInfoDataVO.getYearDataList()) {
                batchYearDataMap.put(DataConvertUtil.parse2Int(projectDetailData.getYear()) + item.getYear() - 1, item);
            }
            resultMap.put("batch-common-param-year", batchYearDataMap);
        }
        if(commonParamData!=null) {
//            resultMap.put("evaluation-common-param-fixed", commonParamData.getCommonFixed());
//            resultMap.put("evaluation-common-param-unit", commonParamData.getCommonUnitCost());

            if(commonParamData.getCommonYearRelatedList()!=null) {
                Map<Integer, CommonYearRelatedVO> commonYearDataMap = new HashMap<>();
                for (CommonYearRelatedVO item : commonParamData.getCommonYearRelatedList()) {
                    commonYearDataMap.put(DataConvertUtil.parse2Int(projectDetailData.getYear()) + item.getYear() - 1, item);
                }
                resultMap.put("evaluation-common-param-year", commonYearDataMap);
            }
        }
        return resultMap;
    }

    @Override
    protected Object transferValue(ModelInputItem item, Object obj) {
        String paramId = item.getId();
        Object data = item.getValue(obj);
        if("evaluation_stage".equals(paramId)) {
            EvaluationMethodEnum methodEnum = EvaluationMethodEnum.getEnum((String) data);
            int value = 0;
            if(methodEnum == null) {
                return value;
            }
            switch (methodEnum) {
                case GROUND:
                    //地面传2
                    value = 3;
                    break;
                case DEDUCT_APPORTIONMENT:
                    //扣分摊传1
                    value = 2;
                    break;
                case CONVENTIONAL:
                case OPTIMIZE_INVEST:
                    //常规和优化投资传0
                    value = 1;
                    break;
                default:
            }
            return value;
        } else if("cost_detail".equals(paramId)) {
            CostEvaMethodEnum methodEnum = CostEvaMethodEnum.getEnum((String) data);
            int value = 0;
            if(methodEnum == null) {
                return value;
            }
            switch (methodEnum) {
                case COST_DETAIL:
                    //成本构成
                    value = 3;
                    break;
                case OIL_QUOTA:
                    //吨油定额
                    value = 2;
                    break;
                case FIX_VARIABLE:
                    // 固定可变
                    value = 4;
                    break;
                default:
                    value = 1;
            }
            return value;
        }  else if("is_cost_detail".equals(paramId)) {
            CostEvaMethodEnum methodEnum = CostEvaMethodEnum.getEnum((String) data);
            if(methodEnum == CostEvaMethodEnum.COST_DETAIL) {
                return true;
            }
            return false;
        }
        return data;
    }


}
